Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reload-failed-proxied-images feature #2393

Merged
merged 19 commits into from
Sep 1, 2019
Merged

Add reload-failed-proxied-images feature #2393

merged 19 commits into from
Sep 1, 2019

Conversation

GramThanos
Copy link
Contributor

@GramThanos GramThanos commented Aug 30, 2019

Description

Many times the badges images fail to load. This feature detects such failed to load images and tries to reload them so that the user don't have to manually reload the hole page.

Test

On the issue badges/shields#1568 there is a 4 seconds delayed badge that will always fail to load (the GitHub's proxy timeouts) you can see on the console tab that the feature will try to reload that image up to 6 times.

image

Preview

reload-failed-proxied-images

reload-failed-proxied-images

source/features/reload-failed-proxied-images.tsx Outdated Show resolved Hide resolved
source/features/reload-failed-proxied-images.tsx Outdated Show resolved Hide resolved
// Handle failure of loading the image
// (maybe, the original image source failed to provide the image to the proxy in the 4 seconds timeframe)
tester.addEventListener('error', () => {
let tries = parseInt(image.dataset.brokenProxiedImage || '0', 10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pass tries into the test function and avoid updating the DOM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if there is a case where init will be fired (by the onAjaxedPages) while the page will still have the same images.

Copy link
Member

@fregante fregante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 on this feature.

The reason why badges might not be loading is due to traffic issues. We should not make the problem worse.

@GramThanos
Copy link
Contributor Author

GramThanos commented Aug 30, 2019

👎 on this feature.

The reason why badges might not be loading is due to traffic issues. We should not make the problem worse.

I get your point but I think the problem is both traffic and cache related. So since the client already made the 1st request, the badges server has already started creating and caching your image, an additional request will not cause that much trouble assuming that the image is cached and ready to be served. Thus we can for example allow only 1 or 2 tries with a bigger delay (maybe 5 seconds).

We can also ask the developers over at https://github.com/badges/shields if this will cause additional problems. Of course, as you said, the point is not to make the problem worse.

@kidonng
Copy link
Member

kidonng commented Aug 30, 2019

@fregante This problem is not limited to badges (based on my experiences).
Though I agree this "is due to traffic issue" to some degree, if @GramThanos can handle it right I would like to have this useful feature. There are lengthy READMEs (and other documents) you just don't want to refresh and wait.

test(image);
}
});
}
Copy link
Member

@fregante fregante Aug 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an additional request will not cause that much trouble assuming that the image is cached and ready to be served.

Let's retry once, then.

If you use https://github.com/fregante/image-promise this whole feature can just be:

async function handleErroredImage({target}): Promise<void> {
	await delay(5000);
	try {
		// A cloned image retries downloading
		// `loadImage` awaits it
		// If successfully loaded, the failed image will be replaced.
		target.replaceWith(await loadImage(target.cloneNode()));
	} catch {}
}

function init(): void {
	delegate('img[src^="https://camo.githubusercontent.com"]', 'error', handleErroredImage);
}

features.add({
	id: __featureName__,
	description: 'Retries downloading images that failed downloading due to GitHub limited proxying.',
	screenshot: false,
	init
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my tests/debugging delegate does not work in this case. I think GitHub gabs the errors for statistics and the event does not bubble up to the body element. Maybe I am wrong.

Copy link
Member

@fregante fregante Aug 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works if you pass a true as the last parameter of delegate to make it a capture listener.

Copy link
Member

@fregante fregante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested but it looks good. Needs a mention in the readme, probably under Repositories

@fregante fregante changed the title Implemented 'Auto reload failed github proxied images' feature Add reload-failed-proxied-images feature Aug 31, 2019
@fregante fregante merged commit 5041571 into refined-github:master Sep 1, 2019
@fregante
Copy link
Member

fregante commented Sep 1, 2019

Thank you ^_^

@paulmelnikow
Copy link

Hi from Shields! I noticed this was mentioned from badges/shields#1568.

I'll give my belated 👍on this as the general alternative is reloading the entire readme which typically forces a reload of all the images. It's both a better experience and overall less traffic to reload just the broken images.

Can I ask whether this reloads the images just once, or if it repeatedly attempts to reload the images? I'd favor the former, or else it'd be a good idea to include some kind of backoff.

By the way, we're tracking the current capacity problem at badges/shields#3874 and hope to have some resolution shortly!

@GramThanos
Copy link
Contributor Author

@paulmelnikow Due to various traffic concerns, it reloads the failed images 5 seconds after the error event, only once.

@fregante fregante mentioned this pull request Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

5 participants